package com.hcrazy.manchester.app.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;

import com.hcrazy.manchester.app.jdbc.MessageDAO;
import com.hcrazy.manchester.app.jdbc.UserDAO;
import com.hcrazy.manchester.app.pojo.Message;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.utils.Constant;


public class MessageService {

	private MessageDAO messageDAO;
	
	private UserDAO userDAO;

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public MessageDAO getMessageDAO() {
		return messageDAO;
	}

	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}
	
	public Object addMessage(Message message) throws Exception{
		Map<String,String> result = new HashMap<String,String>();
		result.put(Constant.SINGLE_RETURN,"true");
		if(null != message){
			if(message.getFromUserId() > 0){
				User u1 = userDAO.getUserById(message.getFromUserId());
				message.setFromUserName(u1.getUsername());
			}
			if(message.getToUserId() > 0){
				User u2 = userDAO.getUserById(message.getToUserId());
				message.setToUserName(u2.getUsername());
			}
		}
		DateTime dt = new DateTime();
		if(null != message){
			message.setSendTime(dt.toString("yyyy-MM-dd HH:mm:ss"));
			messageDAO.addMessage(message);
		}
		return result;
	}
	
	public void recoverMessage(long messageId) throws Exception{
		messageDAO.recoverMessage(messageId);
	}
	
	public Message getMessage(long messageId) throws Exception{
		return messageDAO.getMessage(messageId);
	}
	
	public void deleteMessage(long messageId,int real) throws Exception{
		messageDAO.deleteMessage(messageId, real);
	}
	
	public List<Message> getMessages(int status,long toUserId) throws Exception{
		return messageDAO.getMessages(status, toUserId);
	}
	
	public PageModel getPagedMessages(PageModel model,int messageType) throws Exception{
		PageModel pageModel = messageDAO.getPagedMessages(model, messageType);
		
		return pageModel;
	}
	
	public PageModel getPagedMessages(PageModel model,int messageType,long toUserId,int status) throws Exception{
		PageModel pageModel = messageDAO.getPagedMessages(model, messageType, toUserId, status);
		
		return pageModel;
	}
	
	public PageModel getPagedMessages2(PageModel model,int messageType,long fromUserId,int status) throws Exception{
		PageModel pageModel = messageDAO.getPagedMessages2(model, messageType, fromUserId, status);
		
		return pageModel;
	}
	
	public List<Message> getFriendMessages(int messageType,long fromUserId,long friendId,int status) throws Exception{
		return messageDAO.getFriendMessages(messageType, fromUserId, friendId, status);
	}
}
